package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.Admin_Activity;
import com.apiit.webserviceclient.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

public class MainActivity extends Activity implements OnClickListener {

	// public static String result;
	public EditText text1;
	public EditText text2;
	public Button btn1;
	public Button b1;
	public TextView forgetpassword;
	AlertDialog ad;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		GlobalData.cookies = "";
		b1 = (Button) findViewById(R.id.mainbutton1);
		b1.setOnClickListener(this);

		forgetpassword = (TextView) findViewById(R.id.ftgturpassword);
		forgetpassword.setOnClickListener(this);

		ad = new AlertDialog.Builder(this).create();

		// final Intent i = new Intent(this, Index.class);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == b1) {
			try {
				EditText ed1 = (EditText) findViewById(R.id.maineditText1);
				EditText ed2 = (EditText) findViewById(R.id.maineditText2);

				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace.apiit.lk/index.jsp");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put("title", "//title---");
				xpathMap.put("name",
						"/html[1]/body[1]/table[1]/tr[1]/td[1]/table[2]/tr[1]/td[1]/font[1]/b[1]---");
				request.setXpathMap(xpathMap);

				Map<String, String> parametersMap = new HashMap<String, String>();
				parametersMap.put("UserID", ed1.getText().toString());
				parametersMap.put("Password", ed2.getText().toString());
				parametersMap.put("Submit", "Log+In");
				request.setParametersMap(parametersMap);

				request.setType("POST");

				BPEL_Response respones = bpelProcess.doRequest(request);
				// Intent i= new Intent(MainActivity.class,Index.class);
				// i.putExtra(res, response);
				// startActivity(i);
				String tmp = respones.getResultsMap().get("name");
				GlobalData.username = "";
				char [] username = ed1.getText().toString().toCharArray();
				int tempChars = 0;
				for(int i =0; i<username.length ;i++){
					if(i < 3)
					{
						GlobalData.username = GlobalData.username+ (int)username[i];
					}
					tempChars = i + (int)username[i];
				}
				GlobalData.username = GlobalData.username + tempChars;
				GlobalData.authentication_id_lib_perUser = Long.parseLong(("" + GlobalData.autherntication_id_lib + Long.parseLong(GlobalData.username.trim())).trim());
				GlobalData.authentication_id_pagol_perUser =Long.parseLong(("" +  GlobalData.autherntication_id_pagol + Long.parseLong(GlobalData.username.trim())).trim());
				GlobalData.authentication_id_pagolLec_perUser =Long.parseLong(("" +  GlobalData.autherntication_id_pagolLec + Long.parseLong(GlobalData.username.trim())).trim());
				GlobalData.authentication_id_results_perUser = Long.parseLong(("" + GlobalData.autherntication_id_results + Long.parseLong(GlobalData.username.trim())).trim());
				GlobalData.authentication_id_lmsnew_perUser =Long.parseLong(("" +  GlobalData.autherntication_id_lmsnew + Long.parseLong(GlobalData.username.trim())).trim());
				GlobalData.authentication_id_lms_perUser =Long.parseLong(("" +  GlobalData.autherntication_id_lms + Long.parseLong(GlobalData.username.trim())).trim());
				//GlobalData.authentication_id_leclib_perUser = GlobalData.autherntication_id_lib + Integer.parseInt(GlobalData.username.trim());
				//GlobalData.authentication_id_leclib_perUser = GlobalData.autherntication_id_lib + Integer.parseInt(GlobalData.username.trim());
				if (tmp.contains("CB")) {
					Intent i = new Intent(this, Index.class);
					i.putExtra("", respones.getResultsMap().get("name"));
					startActivity(i);
					Toast.makeText(
							getApplicationContext(),
							"Status: " + respones.getStatus() + " title: "
									+ respones.getResultsMap().get("title")
									+ " name: "
									+ respones.getResultsMap().get("name"),
							Toast.LENGTH_LONG).show();

				} else {

					Intent i = new Intent(this, LecturerIndex.class);
					i.putExtra("", respones.getResultsMap().get("name"));
					startActivity(i);
					Toast.makeText(
							getApplicationContext(),
							"Status: " + respones.getStatus() + " title: "
									+ respones.getResultsMap().get("title")
									+ " name: "
									+ respones.getResultsMap().get("name"),
							Toast.LENGTH_LONG).show();
				}
//
//				ad.show();
//				Intent i = new Intent(this, Index.class);
//				startActivity(i);

			} catch (Exception ex) {
				ad.setTitle("Error!");
				ad.setMessage("The username or password you entered is incorrect");
				ad.show();
				Intent i = new Intent(this, MainActivity.class);
				startActivity(i);
			}

		}
		if (v == forgetpassword) {
			Intent i = new Intent(this, ForgetPassword.class);
			startActivity(i);

		}

	}
}